package handler

import (
	"2302a/weekly/api/request"
	"2302a/weekly/api/resp"
	"2302a/weekly/internal/dao"
	"2302a/weekly/internal/model"
	"fmt"
	"github.com/gin-gonic/gin"
)

func VideoUpload(c *gin.Context) {
	file, _ := c.FormFile("file")
	//ext := path.Ext(file.Filename)
	dst := "D:\\phpstudy_pro\\WWW\\" + file.Filename
	_ = c.SaveUploadedFile(file, dst)
	resp.Success(c, "上传视频成功", fmt.Sprintf("http://localhost/%s", file.Filename))
}

func VideoAdd(c *gin.Context) {
	var req request.VideoAddReq

	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 10000, "参数信息错误", err.Error())
		return
	}
	userId := c.GetUint("userId")
	var user model.User
	dao.GetOneById(userId, &user)
	if user.Id == 0 {
		resp.Error(c, 10000, "用户不存在", false)
		return
	}
	video := model.Video{
		UserId:     userId,
		CoverImage: req.CoverImage,
		VideoUrl:   req.VideoUrl,
		Number:     req.Number,
		Give:       req.Give,
		Title:      req.Title,
		Describe:   req.Describe,
	}
	dao.Create(&video)
	resp.Success(c, "视频发布成功", true)

}

func VideoList(c *gin.Context) {
	var req request.VideoListReq

	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 10000, "参数信息错误", err.Error())
		return
	}

	userId := c.GetUint("userId")
	var user model.User
	dao.GetOneById(userId, &user)
	if user.Id == 0 {
		resp.Error(c, 10000, "用户不存在", false)
		return
	}
	var list []model.Video
	dao.GetListByFinds(&model.Video{Id: req.Id}, &list)

	resp.Success(c, "视频列表成功", list)
}
